package com.ynxd56.westar.analyzer;

import java.text.SimpleDateFormat;
import java.util.List;

import com.ynxd56.westar.dao.TicketDao;
import com.ynxd56.westar.dao.TravelPlanDao;
import com.ynxd56.westar.data.DataFormat;
import com.ynxd56.westar.pojo.Ticket;

public class TicketHandler implements ICommandHandler {

	/**
	 * [0000 1005  1111111111111111 1111111111111111]
	 */
	@Override
	public Object commandAnalysis(String command) {
		String canId=command.substring(25, command.length()-1).trim();
		//TicketDao ticketDao=new TicketDao();
		//Long id=Long.parseLong(canId);
		//Integer id=Integer.parseInt(canId);
		//List<Ticket> list= ticketDao.findByPlanId(id);
		return canId;
	}

	@Override
	public String commandHandler(Object t) {
		Long planId = new TravelPlanDao().findIdByTourcode(t.toString());
		if(planId == null) return null;
		List<Ticket> list = (List<Ticket> )new TicketDao().findByPlanId(planId);
		StringBuffer bf = new StringBuffer();
		for (Ticket ticket : list) {
			bf.append(ticketToString(ticket));
		}
		return bf.toString();
	}

	@Override
	public String packageResult(Object t) {
		
		return DataFormat.packageResult("9005", (String)t);
		
	}

	private String ticketToString(Ticket ticket) {
		StringBuffer bf = new StringBuffer();
		SimpleDateFormat d = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat t = new SimpleDateFormat("HHmmss");
//		SimpleDateFormat t1 = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
//		String sTime = t1.format(ticket.getTime());
		
		String sDate = d.format(ticket.getDate());
		String sTime = t.format(ticket.getTime());
		String hour = DataFormat.formatNumber(String.valueOf(ticket.getHour()),
				3);
		String name = DataFormat.formatString(ticket.getScenicName(), 32);
		String price = DataFormat.formatNumber(
				String.valueOf(ticket.getPrice()), 8);
		String total = DataFormat.formatNumber(
				String.valueOf(ticket.getTotal()), 4);
		String self = DataFormat.formatNumber(String.valueOf(ticket.getSelf()),
				1);
		String note = DataFormat.formatString(ticket.getNote(), 32);
		bf.append(sDate).append(sTime).append(hour).append(name).append(price)
				.append(total).append(self).append(note);
		
		return bf.toString();

	}
	
	public static void main(String[] args){
		String command="[0000100511111111111111112111111111111110]";
		System.out.println(command);
		ICommandHandler ic=new TicketHandler();
		Object t=ic.commandAnalysis(command);
		Object  ts=ic.commandHandler(t);
		
		System.out.println(ic.packageResult(ts));
	}
}
